package juc.container;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author fangbin
 * @desc
 * @date 2020/6/23 7:49
 */
public class ArrayBlockQueueDemo {

    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
        Thread t1 = new Thread(new Interviewer(queue));
        Thread t2 = new Thread(new Consumer(queue));
        t1.start();
        t2.start();
        System.out.println(Math.sqrt(4));
    }
}
class Interviewer implements Runnable{
     BlockingQueue<String> queue;

    public Interviewer(BlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        System.out.println("10个候选人");
        for(int i=0;i<10;i++){
            String candidate = "Candidate"+i;
            try {
                queue.put(candidate);
                System.out.println("安排好了"+candidate);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            queue.put("stop");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Consumer implements Runnable{
    BlockingQueue<String> queue;

    public Consumer(BlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        String msg;
        try {
            while (!(msg = queue.take()).equals("stop")) {
                System.out.println(msg+"到了");
            }
            System.out.println("结束");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}